package com.demo.data;

import com.demo.entity.Role;
import com.demo.entity.User;
import com.demo.util.BCryptUtil;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 模拟数据
 * @date 2021/4/30 16:43
 */
public final class DataMap {
    private static final String rolePrefix = "ROLE_";
    public static User getUser(String username){
        HashMap<String, User> map = new HashMap<>();
        map.put("root", build("root","root","1","2"));
        map.put("user",build("user","user","1"));
        return map.get(username);
    }
    /**
     * 构建
     * @param role 设置权限
     * @date 2021/4/30
     */
    private static User build(String username,String password,String ...role){
        User user = new User();
        user.setUsername(username);
        user.setPassword(BCryptUtil.encode(password));

        ArrayList<Role> list = new ArrayList<>();
        for (String s : role) {
            //用户角色前缀必须得设置
            list.add(new Role(rolePrefix+s));
        }
        user.setRoleList(list);
        return user;
    }


}
